package cn.tedu.tea.service;

import cn.tedu.tea.domain.TOrder;
import cn.tedu.tea.domain.param.TOrderAddNewDTO;
import cn.tedu.tea.domain.param.TOrderUpdateDTO;
import cn.tedu.tea.domain.vo.TOrderListVO;
import cn.tedu.tea.domain.vo.TOrderStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.TOrderMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TOrderServiceImpl implements ITOrderService {

    @Autowired
    private TOrderMapper tOrderMapper;

    @Override
    public void addNew(TOrderAddNewDTO tOrderAddNewDTO) {
        log.debug("开始处理【添加订单付费】的业务，参数： {}", tOrderAddNewDTO);
        int count = tOrderMapper.countByOrderNo(tOrderAddNewDTO.getOrderNo());
        if (count > 0){
            String message = "添加订单付费失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        TOrder tOrder = new TOrder();
        BeanUtils.copyProperties(tOrderAddNewDTO, tOrder);
        int rows = tOrderMapper.insert(tOrder);
        if (rows != 1){
            String message = "添加订单付费失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除订单付费】的业务，参数： {}", id);
        TOrderStandardVO tOrderStandard = tOrderMapper.getStandardById(id);
        if (tOrderStandard == null){
            String message = "删除订单付费失败，要删除的订单付费不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = tOrderMapper.deleteById(id);
        if (rows != 1){
            String message = "删除订单付费失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        log.debug("开始处理【根据id批量删除订单付费】的业务，参数： {}", ids);
        int rows = tOrderMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除订单付费失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(TOrderUpdateDTO tOrderUpdateDTO) {
        log.debug("开始处理【修改订单付费信息】的业务，参数：{}", tOrderUpdateDTO);
        TOrderStandardVO tOrderStandard = tOrderMapper.getStandardById(tOrderUpdateDTO.getId());
        if (tOrderStandard == null){
            String message = "修改订单付费失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = tOrderMapper.countByOrderNoAndNotId(tOrderUpdateDTO.getOrderNo(), tOrderUpdateDTO.getId());
        if (count > 0){
            String message = "修改订单付费失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        TOrder tOrder = new TOrder();
        BeanUtils.copyProperties(tOrderUpdateDTO, tOrder);
        int rows = tOrderMapper.update(tOrder);
        if (rows != 1){
            String message = "修改订单付费失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public TOrderStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询订单付费详情】的业务，参数： {}", id);
        TOrderStandardVO tOrderStandard = tOrderMapper.getStandardById(id);
        if (tOrderStandard == null){
            String message = "查询订单付费详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return tOrderStandard;
    }

    @Override
    public List<TOrderListVO> list() {
        log.debug("开始处理【查询订单付费列表】的业务，无参数");
        List<TOrderListVO> list = tOrderMapper.list();
        return list;
    }
    
}
